import {Component, OnInit, EventEmitter, Output} from '@angular/core';
import {FormGroup, Validators, FormBuilder, FormControl} from "@angular/forms";

@Component({
  selector: 'app-forget-password',
  templateUrl: './forget-password.component.html',
  styleUrls: ['./forget-password.component.scss']
})
export class ForgetPasswordComponent implements OnInit {

  forgetFrom: FormGroup;
  @Output() loginEvent:EventEmitter<any> = new EventEmitter();

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.forgetFrom = this.fb.group({
      email: ['', [Validators.email, Validators.required]],
      password: ['', [Validators.required]],
      confirm: ['', [Validators.required, this.confirmationValidator]],
      captcha: ['', [Validators.required]]
    });
  }

  forgetForm(value){
    console.log(value)
  }

  validateConfirmPassword(){
    Promise.resolve().then(() => this.forgetFrom.controls.confirm.updateValueAndValidity());
  }

  confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (control.value !== this.forgetFrom.controls.password.value) {
      return { confirm: true, error: true };
    }
    return {};
  };


  toLogin(){
    this.loginEvent.emit();
  }

}
